package edu.henu.personnel.dao;

import edu.henu.personnel.entity.Notice;
import edu.henu.personnel.vo.NoticeVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @program: PersonnelProject
 * @description: 公告文章DAO接口
                    * @author: zhuyang
                    *
                    * 懒加载
                    * @Result(property = "catalog", column = "cid", javaType = String.class,
                    * one = @One(select = "edu.henu.personnel.dao.CatalogDao.findCatalogNameById", fetchType = FetchType.LAZY))
                    */

                    @Repository
                    public interface NoticeDao {

                    // 获取所有的公告列表
                    @Select("select nid, cid, ntitle, ndate from t_notice order by nid desc")
                    @Results({
                            @Result(id = true, property = "nid", column = "nid"),
                            @Result(property = "cid", column = "cid"),
                            @Result(property = "catalogName", column = "cid", javaType = String.class,
                                    one = @One(select = "edu.henu.personnel.dao.CatalogDao.findCatalogNameById"))
                    })
                    List<NoticeVO> findAllNotice();

                    // 根据公告id获取响应的notice
                    @Select("select * from t_notice where nid = #{nid}")
                    @Results({
                    @Result(id = true, property = "nid", column = "nid"),
            @Result(property = "cid", column = "cid"),
            @Result(property = "catalogName", column = "cid", javaType = String.class,
                    one = @One(select = "edu.henu.personnel.dao.CatalogDao.findCatalogNameById"))
    })
    Notice findNoticeById(String nid);

    // 根据文章的标题获取指定公告信息
    @Select("select * from t_notice where ntitle = #{ntitle}")
    Notice findNoticeByTitle(String ntitle);

    /**
     * 根据相应的类别id获取其中所有的文章标题和发布时间
     *
     * @param cid
     * @return 返回一个Map，其中存放cid(Integer), ntitle(String), ndate(String)
     */
    @Select("select nid, cid, ntitle, ndate from t_notice where cid = #{cid} order by nid desc")
    @Results({
            @Result(id = true, property = "nid", column = "nid"),
            @Result(property = "cid", column = "cid"),
            @Result(property = "catalogName", column = "cid", javaType = String.class,
                    one = @One(select = "edu.henu.personnel.dao.CatalogDao.findCatalogNameById"))
    })
    List<NoticeVO> findNoticeByCatalogId(Integer cid);


    // 通过栏目id获取9行数据在前台展示
    @Select("select * from t_notice where cid = #{cid} order by nid desc limit 9")
    @Results({
            @Result(id = true, property = "nid", column = "nid"),
            @Result(property = "cid", column = "cid"),
            @Result(property = "catalogName", column = "cid", javaType = String.class,
                    one = @One(select = "edu.henu.personnel.dao.CatalogDao.findCatalogNameById"))
    })
    List<NoticeVO> findNoticeToIndex(Integer cid);

    // 插入一个新的公告
    @Insert("insert into t_notice" +
            "(nid, cid, ntitle, ncontent, ndate, nuser, nbrowses, nappendix1, nappendix2, nappendix3, nappendix4, nappendix5, nappendix6, nmemo) " +
            "values " +
            "(#{nid}, #{cid}, #{ntitle}, #{ncontent}, #{ndate}, #{nuser}, #{nbrowses}, #{nappendix1}, #{nappendix2}, #{nappendix3}, #{nappendix4}, #{nappendix5}, #{nappendix6}, #{nmemo})")
    int insertNotice(Notice notice);

    // 修改公告
    @Update("update t_notice set cid=#{cid}," +
            "ntitle=#{ntitle}," +
            "ncontent=#{ncontent}," +
            "ndate=#{ndate}," +
            "nuser=#{nuser}," +
            "nbrowses=#{nbrowses}," +
            "nappendix1=#{nappendix1}," +
            "nappendix2=#{nappendix2}," +
            "nappendix3=#{nappendix3}," +
            "nappendix4=#{nappendix4}," +
            "nappendix5=#{nappendix5}," +
            "nappendix6=#{nappendix6}," +
            "nmemo=#{nmemo} " +
            "where nid = #{nid}")
    int updateNotice(Notice notice);


    // 删除指定id的公告
    @Delete("delete from t_notice where nid = #{nid}")
    int deleteNotice(@Param("nid") String nid);

    // 增加notice的访问量
    @Update("update t_notice set nbrowses = nbrowses + 1 where nid = #{nid}")
    int addBrowses(String nid);

}
